<?
class ZPlus_Plugin_Menu
{
  public function __construct()
  {
    $this->menu = array();
  }
  
  private static function str(array &$menu)
  {
    $r = '<ul>';
    foreach ($menu as $k => &$v)
    {
      if (is_array($v))
        $r .= '<li>'.$k.self::str($v).'</li>';
      else
        $r .= '<li><a href="'.$v.'">'.$k.'</a></li>';
    }
    $r .= '</ul>';
    return $r;
  }
  
  public function __toString()
  {
    return self::str($this->menu);
  }
  
  public function addUrl($url, $menu)
  {
    if (empty($menu)) return;
    if (!is_array($menu)) $menu = array($menu);
    $target = &$this->menu;
    $last = count($menu) - 1;
    foreach ($menu as $k => &$v)
    {
      if ($k != $last)
      {
        if (!isset($target[$v]) || !is_array($target[$v]))
          $target[$v] = array();
        $target = &$target[$v];
      }
      else
      {
        $target[$v] = $url;
      }
    }
  }

  public function add($plugin, $method, $menu)
  {
    $this->addUrl($_SERVER['SCRIPT_NAME'].'/'.$plugin.'/'.$method, $menu);
  }
}
